#include<bits/stdc++.h>
#define int long long
using namespace std;
int t,n,m,head[2005],d[2005],vis[2005],x1,y,z,tot,h[2005],cnt=0;
queue<int> q;
struct S{
	int v,w,nxt;
}s[3005];
void add(int u1,int v1,int w1){
	tot++;s[tot].v=v1;s[tot].w=w1;s[tot].nxt=head[u1];head[u1]=tot;
}
int read();
void write(int,char='!');
char buf[1 << 25] ,*p1 = buf ,*p2 = buf;
void spfa(){
	memset(d,0x3f,sizeof(d));
	memset(vis,0,sizeof(vis));
	memset(h,0,sizeof(h));
	while(!q.empty()) q.pop();
	d[1]=0;vis[1]=1;q.push(1);
	while(!q.empty()){
		int x=q.front();q.pop();
		vis[x]=0;
		for(int i=head[x];i;i=s[i].nxt){
			if(d[s[i].v]>d[x]+s[i].w){
				d[s[i].v]=d[x]+s[i].w;
				h[s[i].v]=h[x]+1;
				if(h[s[i].v]>=n){
					cnt=1;return;
				}
				if(!vis[s[i].v]){
					q.push(s[i].v);
					vis[s[i].v]=1;
				}
			}
		}
	}
}
inline int read()
{
	int _read_data;
	short _read_f = 1;
	char _read_c;
	while ((_read_c = getchar()) < '0' || _read_c > '9')
		if (_read_c == '-')
			_read_f = -1;
	_read_data = (_read_c ^ 48);
	while ((_read_c = getchar()) >= '0' && _read_c <= '9')
		_read_data = (_read_data << 3) + (_read_data << 1) + (_read_c ^ 48);
	return _read_f * _read_data;
}

inline void write(int _write_data,char _write_end)
{
	if (_write_data < 0) {
		putchar('-');
		_write_data = -_write_data;
	}
	if (_write_data > 9)
		write(_write_data / 10,'!');
	putchar(_write_data % 10 + '0');
	if(_write_end!='!')putchar(_write_end);
}
signed main(){
	t=read();
	while(t--){
		memset(&s,0,sizeof(S));
		tot=0;cnt=0;
		scanf("%d%d",&n,&m);
		for(int j=1;j<=m;j++){
			x1=read();
			y=read();
			z=read();
			add(x1,y,z);
		}
		spfa();
		if(cnt==1) puts("YES\n");
		else puts("NO\n");
	}
	return 0;
}
